<script setup>
import { Edit, Delete } from "@element-plus/icons-vue";
import { ref } from "vue";
import {
  questionTypeListService,
  questionTypeAddService,
  questionTypeUpdateService,
  questionTypeDeleteService,
} from "@/api/question.js";
import { ElMessage, ElMessageBox } from "element-plus";
const type = ref([]);

//声明一个异步函数：
const getQuestuinTypeList = async () => {
  const res = await questionTypeListService();
  type.value = res.data;
};
getQuestuinTypeList();

//控制添加分类弹窗
const dialogVisible = ref(false); //默认不显示

//添加分类数据模型
//模型中的名字 要和接口中的名字一样

const categoryModel = ref({
  name: "",
});
//添加分类表单校验
const rules = {
  name: [{ required: true, message: "请输入分类名称", trigger: "blur" }],
};

//弹窗标题
const title = ref("");

//添加分类
const addQuestionType = async () => {
  //调用接口
  let result = await questionTypeAddService(categoryModel.value);
  ElMessage.success(result.msg ? result.msg : "添加成功");
  //清空添加分类数据
  categoryModel.value = {};
  //刷新页面
  getQuestuinTypeList();
  //关闭弹窗
  dialogVisible.value = false;
};

const updateQuestionType = async () => {
  let result = await questionTypeUpdateService(categoryModel.value);
  ElMessage.success(result.msg ? result.msg : "修改成功");
  categoryModel.value = {};
  //刷新页面
  getQuestuinTypeList();
  //关闭弹窗
  dialogVisible.value = false;
};

const showDialog = (row) => {
  dialogVisible.value = true;
  title.value = "编辑分类";
  categoryModel.value.name = row.name;
  //扩展id属性
  categoryModel.value.id = row.id;
};

// const clearData= ()=>{
//   categoryModel.value.name = '';
// }
const deleteQuestionType =  (row) => {
  ElMessageBox.confirm("你确定要删除这条信息吗？", "温馨提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "警告",
  })
    .then(async () => {
      let result=await questionTypeDeleteService(row.id);
      ElMessage({
        type: "success",
        message: "删除成功",
      });
      //刷新页面
      getQuestuinTypeList();  

    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "删除取消",
      });
    });
  //let result = await questionTypeDeleteService(categoryModel.value.id);
};
</script>
<template>
  <el-card class="page-container transparent-card">
    <template #header>
      <div class="header">
     
        <div class="extra">
          <el-button
            type="primary"
            @click="
              dialogVisible = true;
              title = '添加分类';
              categoryModel.name = '';
            "
            >添加分类</el-button
          >
        </div>
      </div>
    </template>
    <el-table :data="type" style="width: 100%">
      <el-table-column label="序号" width="100" type="index"> </el-table-column>
      <el-table-column label="题目类型" prop="name"></el-table-column>
      <el-table-column label="操作" width="100">
        <template #default="{ row }">
          <!-- 这里的row就是当前这一行的数据 -->
          <el-button
            :icon="Edit"
            circle
            plain
            type="primary"
            @click="showDialog(row)"
          ></el-button>
          <el-button
            :icon="Delete"
            circle
            plain
            type="danger"
            @click="deleteQuestionType(row)"
          ></el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据" />
      </template>
    </el-table>

    <!-- 添加分类弹窗 -->
    <el-dialog v-model="dialogVisible" :title="title" width="30%">
      <el-form
        :model="categoryModel"
        :rules="rules"
        label-width="100px"
        style="padding-right: 30px"
      >
        <el-form-item label="分类名称" prop="name">
          <el-input
            v-model="categoryModel.name"
            minlength="1"
            maxlength="10"
          ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button
            type="primary"
            @click="
              title == '添加分类' ? addQuestionType() : updateQuestionType()
            "
          >
            确认
          </el-button>
        </span>
      </template>
    </el-dialog>
  </el-card>
</template>

<style lang="scss" scoped>
.page-container {
  min-height: 100%;
  box-sizing: border-box;

  .header {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
}
.transparent-card {
  background-color: rgba(255, 255, 255, 0.8); /* 半透明背景 */
  backdrop-filter: blur(10px); /* 模糊效果 */
  border-radius: 8px; /* 圆角 */
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); /* 可选：轻微阴影效果 */
}
</style>
